package com.yuncheng.spcyApi.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.spcyApi.config.SmsConfig;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.sys.SysConfigConstant;
import com.yuncheng.spcyApi.service.ISysConfigService;
import com.yuncheng.spcyApi.vo.common.ReplySmsVo;
import com.yuncheng.spcyApi.vo.sys.SysConfigSmsVo;
import groovy.util.logging.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 短信发送 工具类
 */
@Component
@Slf4j
public class SmsUtils {
    @Resource
    private SmsConfig smsConfig;

    @Resource
    @Lazy
    private ISysConfigService sysConfigService;

    /**
     * 用户扩展码  (4位数字)
     * 查询用户短信回复的时候填入用户扩展码
     * (该扩展码是业务系统向用户发送需要回复的短信的时候由业务系统提供的，
     * 具体参考发送接口send.do描述)
     */
    private final String extendCode = "0384";

    /**
     * 流程短信是否开启
     * @return
     */
    private SysConfigSmsVo verifyFlowSmsIsEnable(){
        SysConfigSmsVo smsVo = new SysConfigSmsVo();
        String smsFlag = Optional.ofNullable(sysConfigService.getSysConfigByCode(SysConfigConstant.FLOW_SMS_FLAG_CONFIG))
                .orElseGet(()->"0");
        if (StringUtils.isBlank(smsFlag) || smsFlag.equals("0")){
            smsVo.setSendFlag(false);

            /** 默认发送短信号码 */
            String defaultPhone = Optional.ofNullable(sysConfigService.getSysConfigByCode(SysConfigConstant.MRFSHM))
                    .orElseGet(()->"18278813781");
            smsVo.setDefaultPhone(defaultPhone);
        }else {
            smsVo.setSendFlag(true);
            smsVo.setDefaultPhone("");
        }

        return smsVo;
    }

    /**
     * 短信记录-短信是否开启
     * @return
     */
    private SysConfigSmsVo verifySmsRecordIsEnable(){
        SysConfigSmsVo smsVo = new SysConfigSmsVo();
        String smsFlag = Optional.ofNullable(sysConfigService.getSysConfigByCode(SysConfigConstant.SMS_FLAG_CONFIG))
                .orElseGet(()->"0");
        if (StringUtils.isBlank(smsFlag) || smsFlag.equals("0")){
            smsVo.setSendFlag(false);

            /** 默认发送短信号码 */
            String defaultPhone = Optional.ofNullable(sysConfigService.getSysConfigByCode(SysConfigConstant.MRFSHM))
                    .orElseGet(()->"18278813781");
            smsVo.setDefaultPhone(defaultPhone);
        }else {
            smsVo.setSendFlag(true);
            smsVo.setDefaultPhone("");
        }

        return smsVo;
    }

    /** 流程发送短信配置 */
   public String sendSms(String phone,String msg) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
       /*String flag = "0";
       List<SysConfig> SMS_FLAG_CONFIG = sysConfigService.list(new LambdaQueryWrapper<SysConfig>()
               .eq(SysConfig::getConfigCode, SysConfigConstant.FLOW_SMS_FLAG_CONFIG));
       if (CollectionUtils.isNotEmpty(SMS_FLAG_CONFIG)){
           flag = Optional.ofNullable(SMS_FLAG_CONFIG.get(0).getConfigValue()).orElseGet(()->"0");
       }
       if (flag.equals("0")){
           List<SysConfig> mrhm = sysConfigService.list(new LambdaQueryWrapper<SysConfig>()
                   .eq(SysConfig::getConfigCode, SysConfigConstant.MRFSHM));
           if (CollectionUtils.isNotEmpty(mrhm)){
               phone = Optional.ofNullable(mrhm.get(0).getConfigValue()).orElseGet(()->"18278813781");
           }else {
               phone = "18278813781";
           }
       }*/

        /** 是否发送短信配置 */
        SysConfigSmsVo smsVo = this.verifyFlowSmsIsEnable();
        if (!smsVo.isSendFlag()){
            phone = smsVo.getDefaultPhone();
        }

//       return smsConfig.getSmsClient().httpSendSms(phone,"【"+ BusinessConstant.Dw_NAME +"】"+msg,BusinessConstant.Dw_NAME,"",extendCode,null);
       return BusinessConstant.SMS_FLAG;
   }

    /** 短信记录-发送配置 */
    public String sendSmsCase(String phone,String msg) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        /*List<SysConfig> SMS_FLAG_CONFIG = sysConfigService.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, SysConfigConstant.SMS_FLAG_CONFIG));
        String flag = "0";
        if (CollectionUtils.isNotEmpty(SMS_FLAG_CONFIG)){
            flag = Optional.ofNullable(SMS_FLAG_CONFIG.get(0).getConfigValue()).orElseGet(()->"0");
        }
        if (flag.equals("0")){
            List<SysConfig> mrhm = sysConfigService.list(new LambdaQueryWrapper<SysConfig>()
                    .eq(SysConfig::getConfigCode, SysConfigConstant.MRFSHM));
            if (CollectionUtils.isNotEmpty(mrhm)){
                phone = Optional.ofNullable(mrhm.get(0).getConfigValue()).orElseGet(()->"18278813781");
            }else {
                phone = "18278813781";
            }
        }*/

        /** 是否发送短信配置 */
        SysConfigSmsVo smsVo = this.verifySmsRecordIsEnable();
        if (!smsVo.isSendFlag()){
            phone = smsVo.getDefaultPhone();
        }

//        return smsConfig.getSmsClient().httpSendSms(phone,msg,"","广西药监局");
//        return smsConfig.getSmsClient().httpSendSms(phone,"【"+ BusinessConstant.Dw_NAME +"】"+msg,BusinessConstant.Dw_NAME,"",extendCode,null);
        return BusinessConstant.SMS_FLAG;
    }

    /**
     * 自定义发送短信
     * @param phone
     * @param msg
     */
    public String sendSmsZdy(String phone,String msg) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
//        return smsConfig.getSmsClient().httpSendSms(phone,"【"+ BusinessConstant.Dw_NAME +"】"+msg,BusinessConstant.Dw_NAME,"",extendCode,null);
        return BusinessConstant.SMS_FLAG;
    }

    /**
     * 获取发送短信人的回复短信
     * @param date 回复日期
     * @return
     */
    public List<ReplySmsVo> getSendSmsMoInfo(String phone, String date){
        /** 返回结果
         * {
         "errNum": "OK",
         "errMsg": "成功",
         "data": [
         {
         "extendCode": "0310",
         "phone": "13317710721",
         "sendTime": "2022-12-22 19:44:00",
         "content": "okok"
         }]}
         */

        /** 是否发送短信配置 */
        /*SysConfigSmsVo smsVo = this.verifyFlowSmsIsEnable();
        if (!smsVo.isSendFlag()){
            phone = smsVo.getDefaultPhone();
        }*/

        /*String mo = smsConfig.getSmsClient().getMo(extendCode, date, phone);

        JSONObject jsonObject = JSONObject.parseObject(mo);
        String errNum = jsonObject.getString("errNum");
        if (StringUtils.isNotBlank(errNum) && !errNum.equals("OK")){

        }
        List<ReplySmsVo> dataList = JSONArray.parseArray(jsonObject.get("data").toString(), ReplySmsVo.class);*/
        return new ArrayList<>(1);
    }
}
